package inventory.controler;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import utilites.Constants;
import utilites.ViewUtilites;
import dto.Gin;
import dto.IssueSlip;
import dto.MaterialMaster;
import dto.MaterialSpecification;
import inventory.daoImplementer.GinImplementor;
import inventory.daoImplementer.GinItemsImplementor;
import inventory.daoImplementer.IssueSlipImplementor;
import inventory.daoImplementer.IssueSlipItemImplementor;
import inventory.daoImplementer.MaterialMasterImplementor;
import inventory.daoImplementer.MaterialSpecificationImplementor;
import inventory.daoInterface.GinDao;
import inventory.daoInterface.GinItemsDao;
import inventory.daoInterface.IssueSlipDao;
import inventory.daoInterface.IssueSlipItemDao;
import inventory.daoInterface.MaterialMasterDao;
import inventory.daoInterface.MaterialSpecificationDao;
import inventory.model.GinItems;
import inventory.model.IssueSlipItem;
import inventory.model.Material;
import inventory.view.Outward;

public class IssueSlipController implements ListSelectionListener,ActionListener
{
	private IssueSlip model;
	private Outward view;
	
	public IssueSlipController(IssueSlip model, Outward view) 
	{
		super();
		this.model = model;
		this.view = view;
		view.addlistlistener(this);
		
		setListSlip();
	}
	
	public IssueSlipController()
	{
		
	}
	
	//*******************************************************************************
	private void setListSlip()
	{
		IssueSlipDao imp=new IssueSlipImplementor();
		
		IssueSlip[] slipArray = null;
		try {
			slipArray = imp.retreiveArray("status", Constants.ACTIVE+"");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String[] items=new String[slipArray.length];
		
		for(int i=0;i<items.length;i++)
			items[i]=slipArray[i].getSlipNo()+","+slipArray[i].getIssueSlipDate();
		
		ViewUtilites.setList(view.getList1(), items);
	}
	
	private void setIsuueSlipItemTable(String selection)
	{
		IssueSlipItemDao items=new IssueSlipItemImplementor();
		
		IssueSlipItem[] issueItems = null;
		try {
			issueItems = items.retreiveArray("slipNo",selection);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String[] data=new String[issueItems.length];
		
		for(int i=0;i<data.length;i++)
		{
			data[i]=issueItems[i].getmCode();
		}
		
		for(int i=0;i<data.length;i++)
		{
			MaterialMasterDao imp=new MaterialMasterImplementor();
			
			MaterialMaster mastere = null;
			try {
				mastere = imp.retreive("mCode",data[i]);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			MaterialSpecificationDao impSpec=new MaterialSpecificationImplementor();
			
			MaterialSpecification specs = null;
			try {
				specs = impSpec.retreive("mCode", data[i]);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			Material materials=new Material(mastere,specs);
			
			issueItems[i].setMaterial(materials);
		}
		
		
		JTable temp=view.getTable();
		int sr=0;
		DefaultTableModel dm=(DefaultTableModel) temp.getModel();
		
		for(IssueSlipItem gin:issueItems)
		{
			dm.addRow(new Object[]{sr,gin.getMaterial().getMname(),gin.getMaterial().getSpecs(),gin.getMaterial().getUnit(),gin.getRequiredQuant(),gin.getIssueQuant()});
			sr++;
		}
	}
	
	public IssueSlip getModel() 
	{
		return model;
	}

	public void setModel(IssueSlip model) {
		this.model = model;
	}

	public Outward getView() {
		return view;
	}

	public void setView(Outward view) {
		this.view = view;
	}
	
	
	public void valueChanged1(ListSelectionEvent arg0) 
	{
		 if (arg0.getValueIsAdjusting())
			 ;
		 String selection= view.getList1().getSelectedValue().toString();	
	}

	//************************************************************************************************************ 
	
	@Override
	public void valueChanged(ListSelectionEvent arg0) 
	{
		if (arg0.getValueIsAdjusting());
		
		else
		 {
			 String selection= view.getList1().getSelectedValue().toString();
			 
			 selection=selection.split(",")[0];
			 
			 setIsuueSlipItemTable(selection);
		 }
	}

	@Override
	public void actionPerformed(ActionEvent arg0) 
	{
		
	}
}
